//----------------------------------------------------------------
#include "Player.h"
#include "World.h"
#include "GameScene.h"
//----------------------------------------------------------------
Player::Player(World* pkWorld, Scene* pkGameScene, Importer* pkImporter, A* pkPathfinder)
:
m_piResourceAmount(NULL),
m_pkWorld(pkWorld),
m_pkGameScene(pkGameScene),
m_pkImporter(pkImporter),
m_pkPathfinder(pkPathfinder)
{
	m_piResourceAmount = new int[ResourceList::Resorce_Count];
	for (int i = 0; i < ResourceList::Resorce_Count; i++)
		m_piResourceAmount[i] = 0;
}
//----------------------------------------------------------------
Player::~Player()
{
	if (m_piResourceAmount)
	{
		delete[] m_piResourceAmount;
		m_piResourceAmount = NULL;
	}

	while (!m_kBases.empty())
	{
		delete m_kBases.begin()->second;
		m_kBases.erase(m_kBases.begin());
	}

	
	while (!m_kMinersVector.empty())
	{
		delete m_kMinersVector[0];
		m_kMinersVector.erase(m_kMinersVector.begin());
	}
	
}
//----------------------------------------------------------------
void Player::setResourceAmount(ResourceList kResType, int iAmount)
{
	m_piResourceAmount[kResType] = iAmount;
}
//----------------------------------------------------------------
int Player::getResourceAmount(ResourceList kResType)
{
	return m_piResourceAmount[kResType];
}
//----------------------------------------------------------------
void Player::addResourceAmount(ResourceList kResType, int iAmount)
{
	m_piResourceAmount[kResType] += iAmount;
	cout << "Player amount food: " << m_piResourceAmount[ResourceList::Food] << endl;
	cout << "Player amount gold: " << m_piResourceAmount[ResourceList::Gold] << endl;
	cout << "Player amount stone: " << m_piResourceAmount[ResourceList::Stone] << endl;
	cout << "Player amount wood: " << m_piResourceAmount[ResourceList::Wood] << endl;
}
//----------------------------------------------------------------
bool Player::addBase(int iPosX, int iPosY)
{
	if (!m_pkWorld->addPlayerBase(iPosX, iPosY))
		return false;

	Base* kBase = new Base(iPosX, iPosY);
	pair<int, int> kPos;
	kPos.first = iPosX;
	kPos.second = iPosY;
	m_kBases[kPos] = kBase;

	return true;
}
//----------------------------------------------------------------
bool Player::addMiner(int iPosX, int iPosY)
{
	pair<int, int> kPos;
	kPos.first = iPosX;
	kPos.second = iPosY;

	BaseIterator itBase = m_kBases.find(kPos);
	if (itBase == m_kBases.end())
	{
		#ifdef DEBUG_TEXT
		cout << "Los mineros deben ser creados en las bases" << endl;
		#endif
		return false;
	}

	Miner* kMiner = new Miner(iPosX, iPosY, m_pkWorld, this);

	if( !m_pkImporter->createSprite("Miner", &kMiner->m_pkSprite) )
		return false;

	m_pkGameScene->addEntity(&kMiner->m_pkSprite);

	m_kMinersVector.push_back(kMiner);

	return true;
}
//----------------------------------------------------------------
void Player::giveMinerCommand(size_t iMinerIndex, Miner::MinerActions kAction)
{
	if ( (iMinerIndex >= 0) && (iMinerIndex < m_kMinersVector.size()) )
	{
		m_kMinersVector[iMinerIndex]->minerDoAction(kAction);
	}
}
//----------------------------------------------------------------
void Player::update(float fTimeBetweenFrames)
{
	for(unsigned int i = 0; i < m_kMinersVector.size(); i++)
		m_kMinersVector[i]->update(fTimeBetweenFrames);
}
//----------------------------------------------------------------
bool Player::getNearestBase(vector<int>& rkPath, int iMinerPosX, int iMinerPosY)
{
	if(m_kBases.empty())
		return false;

	vector<int> kPath;
	int iDistance;
	int iMinDistance;
	bool bAtLeastOne = false;
	for (BaseIterator itNode = m_kBases.begin(); itNode != m_kBases.end(); itNode++)
	{
		if (m_pkPathfinder->calculatePath(itNode->second->getPosX(), itNode->second->getPosY(), iMinerPosX, iMinerPosY, kPath, iDistance))
		{
			if(itNode == m_kBases.begin())
			{
				iMinDistance = iDistance;
				rkPath = kPath;
			}
			if(iDistance < iMinDistance)
			{
				iMinDistance = iDistance;
				rkPath = kPath;
			}
			bAtLeastOne = true;
		}
	}
	return bAtLeastOne;
}
//----------------------------------------------------------------
